package com.start.test.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyqiu.common.tools.FlyQiuCollectionTools;
import com.start.common.dataImport.ImportExportUtils;
import com.start.common.dataRule.DataQueryConstructor;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.PermissionEvaluator;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import com.start.security.annotations.DataPermission;
import com.start.core.annotations.StartLog;
import com.start.test.entity.ActFormTestTableEntity;
import com.start.test.service.ActFormTestTableService;
import com.start.common.api.Result;
import com.start.common.dataImport.DataImportEntity;
import com.start.core.annotations.RegisterMenu;
import com.start.core.annotations.RegisterPermission;

import java.io.IOException;
import java.util.List;

import jakarta.servlet.http.HttpServletResponse;

/**
 * 数据测试
 *
 * @author ${author}
 * @email ${email}
 * @date 2024-05-05 16:17:41
 */
@Slf4j
@RestController
@RequestMapping("test/actFormTestTable")
@RegisterMenu(value = "数据测试", parentId = "", systemCode = "test:actFormTestTable", component = "views/test/actFormTestTable/index", path = "/actFormTestTable/index")
public class ActFormTestTableController {
    private static final String LOG_TITLE = "数据测试";

    @Resource
    private ActFormTestTableService actFormTestTableService;

    /**
     * 列表
     */
    @GetMapping("/page")
    @PreAuthorize("@pas.hasPermission('test:actFormTestTable:list')")
    @DataPermission("test/actFormTestTable/page")
    public Result<?> list(Page<ActFormTestTableEntity> page, ActFormTestTableEntity entity) {
        QueryWrapper<ActFormTestTableEntity> queryWrapper = DataQueryConstructor.createEntity(ActFormTestTableEntity.class);
        return Result.ok(actFormTestTableService.page(page, queryWrapper));
    }


    /**
     * 信息
     */
    @GetMapping("/getById/{id}")
    @StartLog(value = LOG_TITLE, action = "查询")
    @PreAuthorize("@pas.hasPermission('test:actFormTestTable:detail')")
    @RegisterPermission(value = "test:actFormTestTable:detail", name = "查看")
    public Result<?> getById(@PathVariable("id") String id) {
        return Result.ok(actFormTestTableService.getById(id));
    }

    /**
     * 保存
     */
    @PostMapping("/add")
    @StartLog(value = LOG_TITLE, action = "添加")
    @PreAuthorize("@pas.hasPermission('test:actFormTestTable:add')")
    @RegisterPermission(value = "test:actFormTestTable:add", name = "添加")
    public Result<?> save(@RequestBody ActFormTestTableEntity entity) {
            actFormTestTableService.save(entity);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/updateById")
    @StartLog(value = LOG_TITLE, action = "修改")
    @PreAuthorize("@pas.hasPermission('test:actFormTestTable:edit')")
    @RegisterPermission(value = "test:actFormTestTable:edit", name = "修改")
    public Result<?> update(@RequestBody ActFormTestTableEntity entity) {
            actFormTestTableService.updateById(entity);
        return Result.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/deleteById/{id}")
    @StartLog(value = LOG_TITLE, action = "删除")
    @PreAuthorize("@pas.hasPermission('test:actFormTestTable:delete')")
    @RegisterPermission(value = "test:actFormTestTable:delete", name = "删除")
    public Result<?> delete(@PathVariable("id") String id) {
            actFormTestTableService.removeById(id);
        return Result.ok();
    }

    @GetMapping("/downloadTemplate")
    @DataPermission("test/actFormTestTable/list")
    @PreAuthorize("@pas.hasPermission('test:actFormTestTable:downloadTemplate')")
    @RegisterPermission(value = "test:actFormTestTable:downloadTemplate", name = "下载导入模板")
    public void downloadTemplate(HttpServletResponse response) {
        String fileName = "数据测试导入模板.xls";
        try {
            ImportExportUtils.createImportTemplate(ActFormTestTableEntity.class, response, fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/exportExcel")
    @PreAuthorize("@pas.hasPermission('test:actFormTestTable:exportExcel')")
    @RegisterPermission(value = "test:actFormTestTable:exportExcel", name = "导出excel")
    @DataPermission("test/actFormTestTable/list")
    public void exportExcel(HttpServletResponse response) {
        String fileName = "数据测试导出.xls";
        QueryWrapper<ActFormTestTableEntity> queryWrapper = DataQueryConstructor.createEntity(ActFormTestTableEntity.class);
        List<ActFormTestTableEntity> list = actFormTestTableService.list(queryWrapper);
        ImportExportUtils.exportExcel(ActFormTestTableEntity.class, list, response, fileName);
    }


    @PostMapping("/importExcel")
    @PreAuthorize("@pas.hasPermission('test:actFormTestTable:importExcel')")
    @RegisterPermission(value = "test:actFormTestTable:importExcel", name = "导入excel")
    public Result<?> importExcel(@RequestBody DataImportEntity entity) {
        List<String> filePaths = entity.getFilePath();
        if (FlyQiuCollectionTools.isEmpty(filePaths)) return Result.ok();
        List<ActFormTestTableEntity> dateList = ImportExportUtils.importExcelFile(ActFormTestTableEntity.class, filePaths.get(0));
        if (FlyQiuCollectionTools.isEmpty(dateList)) {
            return Result.ok();
        }
            actFormTestTableService.saveBatch(dateList, 30);
        log.info("文件导入:" + JSON.toJSONString(dateList));
        return Result.ok();
    }
}
